set-option -g prefix C-a
set -s escape-time 0

set-window-option -g xterm-keys on
set -g update-environment -r
set -g default-terminal "screen-256color"

set -g @plugin "tmux-plugins/tpm"
#set -g @plugin "soyuka/tmux-current-pane-hostname"

# Tmux normally uses C-b, which is weird. I'm already used to C-a from GNU
# Screen, so that's what we're going to use.
unbind-key C-b
bind-key C-a last-window
bind-key a send-prefix

set-option -g mouse on

# It makes much more sense to me to start numbering windows at 1.
set -g base-index 1

# Use vi-like keys in the command line.
set-option -g status-keys vi
set-option -g bell-action any
set-option -g set-titles on
set-option -g set-titles-string '#H:#S.#I.#P #W #T'
set-option -g visual-bell off

# Use vi-like keys for modes.
setw -g mode-keys vi
setw -g monitor-activity on

# Use vi-like keys to do selection and copying. Ctrl-A Escape starts copy mode,
# 'v' starts text selection and 'y' copies (yanks).
bind-key Escape copy-mode
bind-key [ copy-mode
bind-key -Tcopy-mode-vi v send -X begin-selection
bind-key -Tcopy-mode-vi y send -X copy-selection-and-cancel
bind-key -Tcopy-mode-vi Enter send -X copy-selection-and-cancel
bind-key C-y run "tmux save-buffer - | xclip -i -selection clipboard"
bind-key C-p run "tmux set-buffer \"$(xclip -o -selection clipboard)\"; tmux paste-buffer"

# These symbols are more... Symbolic.
bind-key _ split-window -v
bind-key | split-window -h

# For now I want to use non-magical bindings for pane swapping so that I can get C-{h,j,k,l} in emacs.
bind-key j select-pane -D
bind-key k select-pane -U
bind-key h select-pane -L
bind-key l select-pane -R
bind-key C-j select-pane -D
bind-key C-k select-pane -U
bind-key C-h select-pane -L
bind-key C-l select-pane -R

# Move windows
bind-key { swap-window -t -1
bind-key } swap-window -t +1

# Restore access to the readline "clear screen" key, since I re-bound it above.
#bind-key C-l send-keys 'C-l'

# I like to hold Ctrl between A and D like I used to do in GNU Screen. Pity me.
bind-key C-d detach

bind-key -n C-S-Up resize-pane -U 5
bind-key -n C-S-Right resize-pane -R 10
bind-key -n C-S-Down resize-pane -D 5
bind-key -n C-S-Left resize-pane -L 10

bind-key -n M-Up resize-pane -U 5
bind-key -n M-Right resize-pane -R 10
bind-key -n M-Down resize-pane -D 5
bind-key -n M-Left resize-pane -L 10

# Move to the next or previous window.
bind-key n next-window
bind-key p previous-window

# Status line options.
set-option -g status-justify left
set-option -g status-bg black
set-option -g status-left-length 40

# Pane color options.
set-option -g pane-active-border-fg green
set-option -g pane-active-border-bg black
set-option -g pane-border-fg white
set-option -g pane-border-bg black

# Windows with an activity alert.
setw -g window-status-activity-attr bold
setw -g window-status-activity-bg "red"
set -g status-interval 2

set -g status-left '#[bg=black,fg=#87ff00,bold] #H '
set -g window-status-format '#[bg=black,fg=white] #I.#(pwd="#{pane_current_path}"; echo ${pwd####*/})#F '
set -g window-status-current-format '#[bg=black,fg=white,bright,underscore] #I.#(pwd="#{pane_current_path}"; echo ${pwd####*/}) '
set -g status-right '#[fg=#87ff00] %Y-%m-%d %H:%M #[default]'

# Scrollback buffer size.
set -g history-limit 4096

# Reload the environment with a keystroke.
bind-key r source-file ~/.tmux.conf

# Break and merge windows and panes. ^ pulls a window into a horizontal pane,
# < pulls a window into a vertical pane, and > breaks it back out into its own
# window.
bind-key < split-window -h \; choose-window 'kill-pane ; join-pane -hs %%'
bind-key > break-pane -d
bind-key ^ split-window -v \; choose-window 'kill-pane ; join-pane -vs %%'

if "test ! -d ~/.tmux/plugins/tpm" \
   "run 'git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm && ~/.tmux/plugins/tpm/bin/install_plugins'"

run '~/.tmux/plugins/tpm/tpm'
